//
//  GHAlertView.m
//  Test
//
//  Created by jansh on 16/2/18.
//  Copyright © 2016年 jansh. All rights reserved.
//

#import "GHAlertView.h"

#define kScreenWidth [UIScreen mainScreen].bounds.size.width
#define kScreenHeight [UIScreen mainScreen].bounds.size.height

@interface GHAlertView ()

@property (nonatomic, strong) UIImageView *imageV;

@end

@implementation GHAlertView

+ (GHAlertView *)alertViewWithGHAlertView:(NSString *)title andWithMessage:(NSString *)message cancelButtonWithTitle:(NSString *)canceltitle otherButtonTitles:(NSString *)otherButtonTitles, ...
{
    GHAlertView *alertV = [[GHAlertView alloc] init];
    alertV.titleLabel.text = title;
    alertV.messageLabel.text = message;
    [alertV.button setTitle:canceltitle forState:(UIControlStateNormal)];
    return alertV;
}

- (void)buttonAction:(UIButton *)sender
{
    NSLog(@"1111");
    [self removeFromSuperview];
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.frame = [UIScreen mainScreen].bounds;
        self.alpha = 1;
        self.opaque = NO;
//        self.backgroundColor = [UIColor lightGrayColor];
//        UIVisualEffectView *visualView = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleDark]];
        UIView *visualView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
        visualView.frame = [UIScreen mainScreen].bounds;
        visualView.tag = 100;
        visualView.backgroundColor = [UIColor grayColor];
        visualView.alpha = 0.85;
        visualView.opaque = NO;
        [self addSubview:visualView];
        self.imageV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"white"]];
        self.imageV.bounds = CGRectMake(0, 0, kScreenWidth / 1.5, kScreenHeight / 5);
        self.imageV.center = CGPointMake(kScreenWidth/2, kScreenHeight/2);
        self.imageV.userInteractionEnabled = YES;
        [visualView addSubview:_imageV];
        [self addViews];
    }
    return self;
}

- (void)addViews
{
    self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, self.imageV.frame.size.height*0.1, self.imageV.bounds.size.width, self.imageV.bounds.size.height*0.2)];
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    self.titleLabel.font = [UIFont systemFontOfSize:18 weight:2];
    [self.imageV addSubview:self.titleLabel];
    
    self.messageLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.titleLabel.frame), CGRectGetWidth(self.imageV.frame), CGRectGetHeight(self.imageV.frame)*0.3)];
    self.messageLabel.font = [UIFont systemFontOfSize:15 weight:0];
    self.messageLabel.textAlignment = NSTextAlignmentCenter;
    [self.imageV addSubview:self.messageLabel];

    self.button = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.button setTitleColor:[UIColor colorWithRed:8/255.0 green:91/255.0 blue:226/255.0 alpha:0.8] forState:(UIControlStateNormal)];
    [self.button setTitle:@"确定" forState:(UIControlStateNormal)];
    self.button.titleLabel.font = [UIFont boldSystemFontOfSize:17];
    self.button.frame = CGRectMake(0, CGRectGetMaxY(self.messageLabel.frame)+4, CGRectGetWidth(self.imageV.frame), CGRectGetHeight(self.imageV.frame)*0.4);
    [self.button addTarget:self action:@selector(buttonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.imageV addSubview:self.button];
}

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    UIView *v = [self viewWithTag:100];
    if (CGRectContainsPoint(v.frame, point)) {
        return v;
    } else {
        return nil;
    }
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self performSelector:@selector(removeFromSuperview) withObject:self];
}


@end
